package com.zhairuihao.admin;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.zhairuihao.classc.ClassService;
import com.zhairuihao.common.interceptor.LoginInterceptor;
import com.zhairuihao.common.model.Image;
import com.zhairuihao.image.ImageService;

import java.util.Objects;

/**
 * 本 demo 仅表达最为粗浅的 jfinal 用法，更为有价值的实用的企业级用法 详见 JFinal 俱乐部: http://jfinal.com/club
 *
 * <p>IndexController
 */

@Before(LoginInterceptor.class)
public class AdminController extends Controller {
    @Inject
    ImageService service;
    @Inject
    ClassService classService;

    public void classes() {
        setAttr("data", classService.listAll());
        setAttr("classes", classService.list());
        render("manager-class.html");
    }

    public void images() {
        Integer page = getParaToInt(0, 1);
        Integer paraToInt2 = getParaToInt(1, null);
        Page<Image> paginate;
        if (Objects.isNull(paraToInt2)) {
            paginate = service.paginate(page, 10);
        } else {
            paginate = service.paginate(paraToInt2, page, 10);
        }
        setAttr("page", paginate);
        setAttr("class1", classService.list());
        render("manager-images.html");
    }

    public void rec() {
        setAttr("list", classService.tree());
        render("rec.html");
    }
}
